import pandas as pd
import numpy as np
from tool import read_file_csv
import matplotlib.pyplot as plt
import seaborn as sns

def demand_data(df,*colname):
    data = []
    for i in range(len(
            np.array(
                list(df[colname[0]])
            )
    )):
        ls = []
        for j in range(len(colname)):
            ls.append(np.array(
                list(df[colname[j]])
            )[i])
        data.append(ls)
    return pd.DataFrame(data=data, columns=colname)

def save_photo(title):
    fig = plt.gcf()
    fig_path = f"./photo/sns{title}"
    fig.savefig(fig_path, dpi=400)
    # plt.show()  #临时显示图片
    plt.close(fig)

# 集体关系分析图
def relational_graph(df,title, *colname):
    data = demand_data(df,*colname)
    plt.rcParams["font.sans-serif"] = "SimHei"
    plt.title(title)
    # kind: ked / hist / reg / kde
    # kind:用于控制非对角线上图的类型，可选'scatter'与'reg'
    # diag_kind:用于控制对角线上的图分类型，可选'hist'与'kde'
    sns.pairplot(data, kind="reg",diag_kind="hist", height=5, aspect=0.7,vars=colname)
    save_photo(title)


# 散点图
def graph_stripplot(df,title, *colname):
    data = demand_data(df,*colname)
    plt.rcParams["font.sans-serif"] = "SimHei"
    plt.title(title)
    sns.stripplot(data=data)
    save_photo(title)


# 箱线图
def graph_boxplot(df,title, *colname):
    data = demand_data(df,*colname)
    plt.rcParams["font.sans-serif"] = "SimHei"
    plt.title(title)
    sns.boxplot(data=data)
    save_photo(title)


# 小提琴图 - 白点是中位数，黑色盒型的范围是下四分位点到上四分位点，细黑线表示须。外部形状即为核密度估计
def graph_violinplot(df,title, *colname):
    data = demand_data(df,*colname)
    plt.rcParams["font.sans-serif"] = "SimHei"
    plt.title(title)
    sns.violinplot(data=data)
    save_photo(title)


# 线性关系 - 多个类别的显示
def graph_lmplot(df,title, *colname):
    data = demand_data(df,*colname)
    plt.rcParams["font.sans-serif"] = "SimHei"
    plt.title(title)
    sns.lmplot(x=colname[0],y=colname[1],data=data,hue=colname[2],markers='*')
    save_photo(title)


# 占比情况
def graph_countplot(df,title, *colname):
    data = demand_data(df,*colname)
    plt.rcParams["font.sans-serif"] = "SimHei"
    plt.title(title)
    # sns.countplot(y='作品视角',hue='签约状态', data=data)  # 签约状态数据偏移过大，故不进行效果呈现
    # sns.countplot(y='作品风格',hue='签约状态', data=data)  # 签约状态数据偏移过大，故不进行效果呈现
    sns.countplot(x='作品视角',hue='作品风格', data=data)
    save_photo(title)


def main():
    file_url = "./file/new_book.csv"
    df = read_file_csv(file_url,encoding="gbk")
    relational_graph(df,"集体关系分析图(线性关系图)","全文字数","营养液数","总书评数","当前被收藏数")
    graph_stripplot(df,"小说关联数据分析图(散点图)","全文字数","营养液数","总书评数","当前被收藏数")
    graph_boxplot(df,"小说关联数据分析图(箱线图)","全文字数","营养液数","总书评数","当前被收藏数")
    graph_violinplot(df,"小说关联数据分析图(小提琴图)","全文字数","营养液数","总书评数","当前被收藏数")
    graph_lmplot(df,"小说总书评数和营养液数的作品视角对比图(线性关系图)","总书评数","营养液数","作品视角")
    graph_lmplot(df,"小说总书评数和营养液数的作品风格对比图(线性关系图)","总书评数","营养液数","作品风格")
    graph_lmplot(df,"小说总书评数和营养液数的签约状态对比图(线性关系图)","总书评数","营养液数","签约状态")
    graph_countplot(df,"小说类型数据对比图(柱状图)","作品视角","作品风格","签约状态")
    print("over!!!")


if __name__ == '__main__':
    main()